<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>20.封装日期格式化函数</title>
</head>

<body>
  <script>
    //时间转换成时间戳
    const date = new Date("1996-7-12 08:22:12");
    console.log(date);
    console.log(date.getFullYear());
    console.log(date.getMonth() + 1);//月份从0开始
    console.log(date.getDate()); //日
    console.log(date.getHours());
    console.log(date.getMinutes());
    console.log(date.getSeconds());


    const formatDate = `${date.getFullYear()}年${date.getMonth() + 1}月`;
    console.log(formatDate);
    function dateFormate (date, format = "YYYY-MM-DD HH:mm:ss") {
      const config = {
        YYYY: date.getFullYear(),
        MM:
          date.getMonth() + 1 < 10
            ? `0${date.getMonth() + 1}`
            : date.getMonth() + 1,
        DD: date.getDate(),
        HH: date.getHours(),
        mm: date.getMinutes(),
        ss: date.getSeconds(),
      }
      for (const key in config) {
        format = format.replace(key,config[key]);
      }
      return format;
    }

    console.log(dateFormate(date,'YYYY年MM月DD日'));
  </script>
</body>

</html>